//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CustomerDlg.h"
#include "CheckingDlg.h"
#include "FormUtilities.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmCustDlg::TfrmCustDlg(TComponent* Owner, TCustomerBridge * bridge)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->Caption = "Add new customer";
   this->bridge = bridge;
   checkings = new TStringList();
   checkings->Duplicates = dupAccept;

   lblIDValue->Caption = IntToStr( bridge->GetLastID() + 1);
   isUpdate = false;

   SetButtonsState();
}
//---------------------------------------------------------------------------

_fastcall TfrmCustDlg::TfrmCustDlg(TComponent* Owner, TCustomerBridge * bridge, __int64 id, AnsiString customerID, AnsiString name, AnsiString customerLastName, AnsiString phone, AnsiString address, AnsiString email)
        : TForm(Owner)

{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->Caption = "Update customer";
   this->bridge = bridge;

   checkings = new TStringList();
   checkings->Duplicates = dupAccept;

   lblIDValue->Caption = IntToStr( id);
   edtCustomerID->Text = customerID;
   edtName->Text = name;
   edtLastName->Text = customerLastName;
   edtPhone->Text = phone;
   edtAddress->Text = address;
   edtEmail->Text = email;

   bridge->GetCheckings( id, checkings);

   RefreshGrid();

   isUpdate = true;
}

//---------------------------------------------------------------------------

void __fastcall TfrmCustDlg::btnOKClick(TObject *Sender)
{
   if( edtName->Text.Length() == 0)
   {
        MessageDlg( "Customer name should be entered !",
                    mtError,
                    TMsgDlgButtons() << mbOK,
                    0);
                    
        edtName->SetFocus();
        ModalResult = mrNone;
        return;
   }

   if( isUpdate)
   {
      bridge->Update( StrToInt64( lblIDValue->Caption),
                      edtCustomerID->Text,
                      edtName->Text,
                      edtLastName->Text,
                      edtPhone->Text,
                      edtAddress->Text,
                      edtEmail->Text,
                      checkings);
   }
   else
   {
      bridge->Add( bridge->GetNextID(),
                   edtCustomerID->Text,
                   edtName->Text,
                   edtLastName->Text,
                   edtPhone->Text,
                   edtAddress->Text,
                   edtEmail->Text,
                   checkings);
   }
}

//---------------------------------------------------------------------------

void TfrmCustDlg::SetButtonsState()
{
   btnUpdate->Enabled = ( checkings->Count > 0);
   btnDelete->Enabled = ( checkings->Count > 0);
}

//---------------------------------------------------------------------------

void TfrmCustDlg::RefreshGrid()
{
   TChecking * checking;
   int rowCount;
   int i;

   for ( i = 1; i < grdCheckings->RowCount; i++)
      grdCheckings->Rows[ i]->Clear();

   if ( checkings->Count <= 1)
      rowCount = 2;
   else
      rowCount = checkings->Count + 1;

   grdCheckings->RowCount = rowCount;

   for( i = 0; i < checkings->Count; i++)
   {
      checking = ( TChecking*)checkings->Objects[ i];
      grdCheckings->Cells[ 0][ i + 1] = checking->right;
      grdCheckings->Cells[ 1][ i + 1] = checking->left;
      if (( double)checking->date != 0)
         grdCheckings->Cells[ 2][ i + 1] = DateToStr( checking->date);
   }

   SetButtonsState();
}

//---------------------------------------------------------------------------

void __fastcall TfrmCustDlg::btnUpdateClick(TObject *Sender)
{
   TfrmCheckingDlg * checkingDlg;
   TChecking * checking;

   checking = ( TChecking *)checkings->Objects[ grdCheckings->Row - 1];

   checkingDlg = new TfrmCheckingDlg( this, checkings, checking);
   
   if ( checkingDlg->ShowModal() == mrOk)
      RefreshGrid();

   delete checkingDlg;
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustDlg::btnAddClick(TObject *Sender)
{
   TfrmCheckingDlg * checkingDlg;

   checkingDlg = new TfrmCheckingDlg( this, checkings);

   if ( checkingDlg->ShowModal() == mrOk)
      RefreshGrid();

   delete checkingDlg;
}

//---------------------------------------------------------------------------

void __fastcall TfrmCustDlg::btnDeleteClick(TObject *Sender)
{
   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
      delete checkings->Objects[ grdCheckings->Row - 1];
      checkings->Delete( grdCheckings->Row - 1);
      RefreshGrid();
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustDlg::FormDestroy(TObject *Sender)
{
   int i;

   for( i = 0; i < checkings->Count; i++)
      delete checkings->Objects[ i];
   checkings->Clear();
   delete checkings;
}
//---------------------------------------------------------------------------

void __fastcall TfrmCustDlg::FormShow(TObject *Sender)
{
   grdCheckings->Cells[ 0][0] = "RIGHT";
   grdCheckings->Cells[ 1][0] = "LEFT";
   grdCheckings->Cells[ 2][0] = "DATE";
}
//---------------------------------------------------------------------------



void __fastcall TfrmCustDlg::grdCheckingsKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------




